package com.elm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.elm.model.Seller;
import com.elm.service.SellerService;
import com.elm.serviceimpl.SellerServiceImpl;
import com.elm.util.PageUtil;
import com.elm.model.PageBean;

public class EnterUserMainController extends HttpServlet{
	SellerService ss = new SellerServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		String p =  req.getParameter("page");
		HttpSession session = req.getSession();
		String shopname = req.getParameter("shopname");
		String shopgenre = req.getParameter("shopgenre");
		  int  page = 0;
		  if(p==null){
			  page=1;
		  }else{
			   page = Integer.parseInt(p);
		  }
		  PageBean   pagebean = new PageBean(page, 9);
		List<Seller> sellers = new ArrayList<>();
		if((shopname==null||shopname=="")&&(shopgenre==null||shopgenre=="")){
		sellers = ss.getAllSellers(pagebean);
		
		session.setAttribute("page", PageUtil.getPage(ss.getSellercount(), page, pagebean.getPageSize()));
		}else{
			
			sellers= ss.findSellerByNameOrGenre(shopname, shopgenre, pagebean);
			session.setAttribute("page", PageUtil.getPage(ss.getSellercount(), page, pagebean.getPageSize()));
		}
		
		req.setAttribute("allsellers", sellers);
		req.getRequestDispatcher("usermain.jsp").forward(req, resp);
	}
}
